﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.AutoMapper;
using MyCompanyName.AbpZeroTemplate.Prm.Admin.Dto;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class BusinessAppService : AbpZeroTemplateAppServiceBase, IBusinessAppService
    {
        IRepository<Business, int> businessRepository;
        IRepository<RdCenter, int> rdCenterRepository;
        IRepository<Laboratory, int> laboratoryRepository;

        public BusinessAppService(
            IRepository<Business, int> _businessRepository,
            IRepository<RdCenter, int> _rdCenterRepository,
            IRepository<Laboratory, int> _laboratoryRepository)
        {
            businessRepository = _businessRepository;
            rdCenterRepository = _rdCenterRepository;
            laboratoryRepository = _laboratoryRepository;
        }

        public List<Dto.GetBusinessOutPut> GetAll()
        {
            var all = businessRepository.GetAll();
            var model = all.MapTo<List<Dto.GetBusinessOutPut>>();
            return model;
        }

        public List<KeyValuePair<long, string>> GetUsers(int businessId, int laboratoryId, int rdCenterId)
        {
           var users= UserManager.Users.AsQueryable();
           if (laboratoryId > 0)
           {
               users = users.Where(x => x.LaboratoryId == laboratoryId);
           }
           else if (rdCenterId > 0)
           {
               users = users.Where(x => x.RdCenterId == rdCenterId);
           }
           else
           {
               users = users.Where(x => x.BusinessId == businessId);
           }

           var results = users.ToList().Select(x => new KeyValuePair<long, string>(x.Id, x.Name)).ToList();
            return results;
        }

        public async Task CreateOrEdit(Dto.CreateOrEditBusinessInPut input)
        {
            var bus = new Business();
            if (input.Id > 0)
            {
                bus = await businessRepository.GetAsync(input.Id);
            }
            else
            {
                await businessRepository.InsertAsync(bus);
            }
            bus.Name = input.Name;
            bus.Principal = input.Principal;
        }

        public async Task<Dto.GetBusinessOutPut> Get(int businessId)
        {
            var bus = await businessRepository.GetAsync(businessId);
            var model = bus.MapTo<Dto.GetBusinessOutPut>();
            return model;
        }


        public async Task<List<Dto.GetCompanyLevelTreeOutPut>> GetCompanyLevel()
        {
            List<Dto.GetCompanyLevelTreeOutPut> result = new List<Dto.GetCompanyLevelTreeOutPut>();
            var business = await businessRepository.GetAllListAsync();
            var rdCenter = await rdCenterRepository.GetAllListAsync();
            var laboratory = await laboratoryRepository.GetAllListAsync();
            foreach (var bus in business)
            {
                var busId = Guid.NewGuid();
                var _bus = new Dto.GetCompanyLevelTreeOutPut()
                {
                    id = busId,
                    businessId = bus.Id,
                    rdCenterId = 0,
                    laboratoryId = 0,
                    leaf = false,
                    principal = bus.Principal,
                    parentId = "0",
                    companyType = Dto.CompanyType.事业部,
                    text = bus.Name,
                    children = new List<Dto.GetCompanyLevelTreeOutPut>()
                };
                foreach (var center in rdCenter.Where(x => x.BusinessId == bus.Id).OrderBy(x => x.Name))
                {
                    var centerId = Guid.NewGuid();
                    var _center = new Dto.GetCompanyLevelTreeOutPut()
                    {
                        id = centerId,
                        businessId = bus.Id,
                        rdCenterId = center.Id,
                        laboratoryId = 0,
                        principal = center.Principal,
                        leaf = false,
                        companyType = Dto.CompanyType.研发中心,
                        parentId = busId.ToString(),
                        text = center.Name,
                        children = new List<Dto.GetCompanyLevelTreeOutPut>()
                    };
                    _bus.children.Add(_center);
                    foreach (var lab in laboratory.Where(x => x.RdCenterId == center.Id).OrderBy(x => x.Name))
                    {
                        var labId = Guid.NewGuid();
                        var _lab = new Dto.GetCompanyLevelTreeOutPut()
                        {
                            id = labId,
                            businessId = bus.Id,
                            rdCenterId = center.Id,
                            laboratoryId = lab.Id,
                            leaf = true,
                            companyType = Dto.CompanyType.研究室,
                            principal = lab.Principal,
                            parentId = centerId.ToString(),
                            text = lab.Name,
                            children = new List<Dto.GetCompanyLevelTreeOutPut>()
                        };
                        _center.children.Add(_lab);
                    }
                }
                result.Add(_bus);
            }
            return result;
        }


        public async Task Delete(int businessId)
        {
            await businessRepository.DeleteAsync(businessId);
        }


        public List<Dto.GetCompanyUserOutPut> GetCompanyUser(GetCompanyInPut input)
        {
            List<Dto.GetCompanyUserOutPut> result = new List<Dto.GetCompanyUserOutPut>();

            var users = UserManager.Users;

            switch (input.CompanyType)
            {
                case MyCompanyName.AbpZeroTemplate.Prm.Admin.Dto.CompanyType.事业部:
                    users = users.Where(m => m.BusinessId == input.BusinessId && m.RdCenterId == 0 && m.LaboratoryId == 0);
                    break;
                case MyCompanyName.AbpZeroTemplate.Prm.Admin.Dto.CompanyType.研发中心:
                    users = users.Where(m => m.RdCenterId == input.RdCenterId && m.LaboratoryId == 0 && m.BusinessId != 0);
                    break;
                case MyCompanyName.AbpZeroTemplate.Prm.Admin.Dto.CompanyType.研究室:
                    users = users.Where(m => m.LaboratoryId == input.LaboratoryId && m.LaboratoryId != 0 && m.BusinessId != 0);
                    break;
                default:
                    break;
            }

            result = users.Select(m => new Dto.GetCompanyUserOutPut()
            {
                Id = m.Id,
                Name = m.Name,
                UserName = m.UserName
            }).ToList();
            return result;
        }


        public void SetCompanyUser(SetCompanyUserInPut input)
        {
            var user = UserManager.Users.FirstOrDefault(m => m.Id == input.UserId);

            switch (input.CompanyType)
            {
                case CompanyType.事业部:
                    user.BusinessId = input.BusinessId;
                    user.RdCenterId = 0;
                    user.LaboratoryId = 0;
                    break;
                case CompanyType.研发中心:
                    user.BusinessId = input.BusinessId;
                    user.RdCenterId = input.RdCenterId;
                    user.LaboratoryId = 0;
                    break;
                case CompanyType.研究室:
                    user.BusinessId = input.BusinessId;
                    user.RdCenterId = input.RdCenterId;
                    user.LaboratoryId = input.LaboratoryId;
                    break;
                default:
                    break;
            }
        }


        public void RemoveCompanyUser(SetCompanyUserInPut input)
        {
            var user = UserManager.Users.FirstOrDefault(m => m.Id == input.UserId);

            switch (input.CompanyType)
            {
                case CompanyType.事业部:
                    user.BusinessId = 0;
                    break;
                case CompanyType.研发中心:
                    user.BusinessId = 0;
                    user.RdCenterId = 0;
                    break;
                case CompanyType.研究室:
                    user.BusinessId = 0;
                    user.RdCenterId = 0;
                    user.LaboratoryId = 0;
                    break;
                default:
                    break;
            }
        }
    }
}
